var request = require('request');
module.exports = {
	get: function(req, res) {
		var data = req.query;
		console.log("data:,", data);
		var date = new Date();
		var year = date.getFullYear();
		var month = (date.getMonth() + 1 > 9) ? (date.getMonth() + 1) : ("0" + (date.getMonth() + 1));
		var days = (date.getDate() > 9) ? date.getDate() : ("0" + date.getDate());
		var hours = (date.getHours() > 9) ? date.getHours() : ("0" + date.getHours());
		var minutes = (date.getMinutes() > 9) ? date.getMinutes() : ("0" + date.getMinutes());
		var seconds = (date.getSeconds() > 9) ? date.getSeconds() : ("0" + date.getSeconds());
		var _time = year + "" + month + "" + days + "" + hours + "" + minutes + "" + seconds;
		console.log("get start!"); // 从这里开始
		var url = 'http://route.showapi.com/9-2'; // 城市访问天气接口
		var form = {
			showapi_appid: '5653',
			showapi_sign: "f668fa7e626043b19b34a61743fcf271",
			showapi_timestamp: _time,
			needMoreDay: 1,
			needIndex: 1,
			from: 3
		};
		if (data.area) {
			form.area = data.area;
		} else if (data.lat) {
			url = "http://route.showapi.com/9-5"; // 坐标访问天气接口
			form.lat = data.lat;
			form.lng = data.lng;
		};
		if ((!data.area) && (!data.lat)) {
			console.log("null?", data);
			res.send("参数错误！,area不可为空，或lat和lng不可为空");
			return;
		};
		request.post({
				url: url,
				form: form,
				encoding: 'utf8'
			},
			function(error, response, body) {
				if (response.statusCode == 200) {
					console.log("get weather success");
					// console.log("response!");
					// console.log(JSON.parse(body))
					res.send(JSON.parse(body).showapi_res_body);
				} else {
					console.log("failed",response.statusCode);
					res.send("sorry,no found!");
				}
			});
		console.log("get end!");
	}
};